import java.util.ArrayList;
import java.util.Stack;

/**
 * IDE: Eclipse
 *
 * @author Justin Niles
 * @author Grant Foster
 * @author Bryce Langlotz
 * @author Max Hanosky
 *
 */
public class CVSearch {

	//float xMin, xMax, yMin, yMax, zMin, zMax;

	private int xGridLength, yGridLength, zGridLength;

	private int[][][] resGrid;
	private int[][][] newGrid = resGrid;

	public CVSearch(int[][][] resGrid, int xGridLength, int yGridLength, int zGridLength /*float xMin, float xMax, float yMin, float yMax, float zMin, float zMax*/) {
		this.resGrid = resGrid;
		/*
		this.xMin = xMin;
		this.xMax = xMax;
		this.yMin = yMin;
		this.yMax = yMax;
		this.zMin = zMin;
		this.zMax = zMax;
		*/
		this.xGridLength = xGridLength;
		this.yGridLength = yGridLength;
		this.zGridLength = zGridLength;
	}

	public int getType(Point point)
	{
		int x = point.x();
		int y = point.y();
		int z = point.z();

		if (isValid(point)) {
			return resGrid[x][y][z];
		}

		return 1;
	}

	public void setType(Point point, int newType) {
		int x = point.x();
		int y = point.y();
		int z = point.z();

		if ((getType(point) != 1) && (isValid(point))) {
			newGrid[x][y][z] = newType;
		}
	}

	public boolean isValid(Point point) {
		return (((point.x() > 0) && (point.x() < xGridLength))
				&& ((point.y() > 0) && (point.y() < yGridLength))
				&& ((point.z() > 0) && (point.z() < zGridLength)));
	}

	public void findVoids() {
		for (int y = 0; y < yGridLength; y++) {
			for (int z = 0; z < zGridLength; z++) {
				probe(new Point(0, y, z));
				probe(new Point(xGridLength, y, z));
			}
		}

		for (int x = 0; x < xGridLength; x++) {
			for (int z = 0; z < zGridLength; z++) {
				probe(new Point(x, 0, z));
				probe(new Point(x, yGridLength, z));
			}
		}

		for (int y = 0; y < yGridLength; y++) {
			for (int x = 0; x < xGridLength; x++) {
				probe(new Point(x, y, 0));
				probe(new Point(x, y, zGridLength));
			}
		}
	}

	public void probe(Point startPoint) {
		Stack<Point> path = new Stack<Point>();
		path.push(startPoint);
		while (!path.isEmpty()) {
			Point current = path.peek();
			if (getType(current.north()) == 0) {
				path.push(current.north());
			}
			else if (getType(current.east()) == 0) {
				path.push(current.east());
			}
			else if (getType(current.south()) == 0) {
				path.push(current.south());
			}
			else if (getType(current.west()) == 0) {
				path.push(current.west());
			}
			else if (getType(current.up()) == 0) {
				path.push(current.up());
			}
			else if (getType(current.down()) == 0) {
				path.push(current.down());
			}
			else {
				setType(current, 2);
				path.pop();
				System.out.println("test");
			}
		}
	}

	public ArrayList findCavities()
	{
	    ArrayList<Point> cavities = new ArrayList<Point>();

	    for (int y = 0; y < yGridLength; y++) {
            for (int z = 0; z < zGridLength; z++) {
                for (int x = 0; x < xGridLength; x++) {
                    Point current = new Point(x, y, z);
                    if (getType(current) == 0)
                    {
                        cavities.add(current);
                    }
                }
            }
        }

	    return cavities;
	}


}
